import {
  PlaneGeometry,
  GridHelper,
  MeshPhongMaterial,
  Mesh,
  TextureLoader,
  RepeatWrapping
} from 'three'
import { Reflector } from './Reflector.js' // 镜面
import { Water } from 'three/examples/jsm/objects/Water.js'

/**
 * 地板地面类
 */
export default class Floors {
  constructor(_viewer) {
    this.viewer = _viewer
    this.scene = _viewer.scene
  }

  /**
   * 添加网格地面
   * @param {object} opts 网格配置选项
   * size -- 坐标格尺寸.
   * divisions -- 坐标格细分次数.
   * colorCenterLine -- 中线颜色.
   * colorGrid -- 坐标格网格线颜色.
   */
  createGrid(opts = {}) {
    const size = opts.size || 40
    const divisions = opts.divisions || 200
    const colorCenterLine = opts.colorCenterLine || '#0471bb'
    const colorGrid = opts.colorGrid || '#0471bb'
    const opacity = opts.opacity || 0.3
    const positionY = opts.positionY || -0.1
    const grid = new GridHelper(size, divisions, colorCenterLine, colorGrid)
    grid.material.opacity = opacity
    grid.material.transparent = true
    grid.position.set(0, positionY, 0)
    this.scene.add(grid)
  }

  createReflector() {
    const geometry = new PlaneGeometry(1000, 1000)
    const groundMirror = new Reflector(geometry, {
      clipBias: 1,
      textureWidth: window.innerWidth * window.devicePixelRatio,
      textureHeight: window.innerHeight * window.devicePixelRatio,
      color: 0xFFFFFF
    })
    groundMirror.rotateX(-Math.PI / 2)
    groundMirror.position.set(0, -0.1, 0)
    this.scene.add(groundMirror)
  }

  createPlane() {
    const waterGeometry = new PlaneGeometry(1000, 1000)
    // 水平、垂直重复次数*
    const material = new MeshPhongMaterial({
      map: new TextureLoader().load('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fxz.huashengben.com%2Fd%2Ftu%2F2020%2F01%2F26%2F5ce3c7cfc79ca.jpg&refer=http%3A%2F%2Fxz.huashengben.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1668432307&t=4cfbe1b599d923056535dcfecb0f058f', function(texture) {
        texture.wrapS = texture.wrapT = RepeatWrapping
        // uv两个方向纹理重复数量
        texture.repeat.set(100, 100)
      }),
      transparent: true,
      depthTest: true,
      depthWrite: false, // 无法被选择，鼠标穿透
      opacity: 0.8
    })
    const mesh = new Mesh(waterGeometry, material)
    mesh.castShadow = true
    mesh.receiveShadow = true
    mesh.rotation.x = -Math.PI / 2
    this.scene.add(mesh)
  }

  createWater(size = [10000, 10000], position = [0, 0, 0], speed = 1) {
    const waterGeometry = new PlaneGeometry(size[0], size[1])
    this.water = new Water(
      waterGeometry, {
        textureWidth: 512,
        textureHeight: 512,
        waterNormals: new TextureLoader().load('/assets/textures/waternormals.jpg', function(texture) {
          texture.wrapS = texture.wrapT = RepeatWrapping
        }),
        waterColor: 0x001e0f,
        distortionScale: 3.7
      }
    )
    this.water.rotation.x = -Math.PI / 2
    this.water.position.set(position[0], position[1], position[2])
    this.water.material.uniforms.sunDirection.value.normalize()
    this.scene.add(this.water)
    this.viewer.addAnimate({
      fun: function run(water) {
        water.material.uniforms.time.value += speed / 1000
      },
      content: this.water
    })
  }
}
